package com.example.administrator.swredeem.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.alpamayo.piccreaderid.DicotCardInfo;
import com.alpamayo.piccreaderid.DicotReader;
import com.alpamayo.piccreaderid.OnDicotReadCardListener;
import com.example.administrator.swredeem.R;

import java.lang.reflect.Field;

public class RecognitionCardActivity extends AppCompatActivity{
    private DicotReader dicotReader;
    String title = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recognition_card);
        dicotReader = new DicotReader(this, "69e7a610-9e78-42cd-8673-9115620e51d5", "47.93.120.91", 1710);
        //身份证服务已运行,setTesting() 设置连接模式为测试，代码测试正常后移除此方法
        dicotReader.setTesting();
        title = getIntent().getStringExtra("title");
    }

    public void goUserList(View view) {

        readIc();
//        Intent intent =null;
//        intent = new Intent(RecognitionCardActivity.this,UserList.class);
//        intent.putExtra("title",title);
//        startActivity(intent);

    }


    public void readIc() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    dicotReader.readCard(new OnDicotReadCardListener() {

                        @Override
                        public void onFinish(int i, DicotCardInfo dicotCardInfo) {

                            Message message = new Message();
                            message.what = i;
                            message.obj = dicotCardInfo;
   
                            baseHandler.sendMessage(message);
                            Log.e("wrc", "读身份证：" + i);

                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e("wrc", "初始化读身份证异常：" + e.getMessage());
                }

            }
        }).start();
    }
    Handler baseHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {

            super.handleMessage(msg);

            switch (msg.what) {


                case DicotReader.RESULT_SUCCESS:
                    Toast.makeText(RecognitionCardActivity.this, "身份证信息获取成功！", Toast.LENGTH_LONG).show();

                    DicotCardInfo var2 = (DicotCardInfo) msg.obj;
                    Class cls = var2.getClass();
                    Field[] fields = cls.getDeclaredFields();
                    for (int i = 0; i < fields.length; i++) {
                        Field f = fields[i];
                        f.setAccessible(true);
                        try {
                            System.out.println("属性名:" + f.getName() + " 属性值:" + f.get(var2));

                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                    Intent intent = new Intent(RecognitionCardActivity.this,UserList.class);
                    intent.putExtra("idCrad",var2.getNumber());
                    startActivity(intent);
                    Log.e("wrc", "身份证读取成功：" + var2.getName());
                    break;

                case DicotReader.RESULT_CARD_DISCONNECT:
                    Toast.makeText(RecognitionCardActivity.this, "身份证接触不良！", Toast.LENGTH_LONG).show();

                    break;
                case DicotReader.RESULT_CARD_UNFIND:
                    Toast.makeText(RecognitionCardActivity.this, "未检测到身份证！", Toast.LENGTH_LONG).show();

                    break;
                case DicotReader.RESULT_NETWORK_TIMEOUT:
                    Toast.makeText(RecognitionCardActivity.this, "网络连接超时！", Toast.LENGTH_LONG).show();

                    break;
                case DicotReader.RESULT_NO_ATHORIZED:
                    Toast.makeText(RecognitionCardActivity.this, "未授权！", Toast.LENGTH_LONG).show();

                    break;
                case DicotReader.RESULT_ATHORIZED_ERROR:
                    Toast.makeText(RecognitionCardActivity.this, "授权错误，请在这里判断是否无网络连接，网络正常请联系身份证识别服务商！", Toast.LENGTH_LONG).show();
                    break;
                default:
                    Toast.makeText(RecognitionCardActivity.this, "未知错误！", Toast.LENGTH_LONG).show();
                    break;

            }

        }
    };

}
